#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
****************************************************
Project:python
Filename: weather_oil.py
Author:银河
Create: 2025/5/9 下午8:57 
Notes:
cron: 0 7 * * *
***************************************************
'''

import time
import requests

class WeatherOil:
    def __init__(self):
        self.url = "http://yi.xuenn.cn/api/weather/oil/"
        self.log_url = "http://yi.xuenn.cn/sys/add/run/logs/"
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
        }

    def get_monitor(self):
        res = requests.get(url=self.url,headers=self.headers).json()
        return res

    def run_log(self):
        t1 = time.time()
        response = self.get_monitor()
        print(response)
        t2 = time.time()

        data = {
            "name": "推送天气预报及油价",
            "content": str(response),
            "rtime": round(t2-t1,3),
        }
        if response['code'] == 200:
            data["isNormal"] = 1
        else:
            data["isNormal"] = 0

        requests.post(url=self.log_url,headers=self.headers,data=data)
        print('新增成功')
    def main(self):
        self.run_log()

if __name__ == '__main__':

    mon = WeatherOil()
    mon.main()
